
public class Deslocamento implements Expressao {

	private Expressao expr1;
	private Expressao expr2;
	
	private String acao;
	private Integer regFinal;
	
	public Deslocamento(Expressao expr1, Expressao expr2, String acao) {
		super();
		this.expr1 = expr1;
		this.expr2 = expr2;
		this.acao = decodificaAcao(acao);
	}

	private String decodificaAcao(String acao) {
		if (acao.equals(">>")) return "DIV";
		if (acao.equals("<<")) return "MUL";

		return "";
	}

	@Override
	public String getCode() {
		StringBuffer sb = new StringBuffer();
		regFinal = ParserUtils.novoRegistrador();
		
		String s1 = expr1.getCode();
		
		if (expr1.possuiRegistradorFinal()) {
			sb.append(s1);
			sb.append(System.lineSeparator());
			
			s1 = "R" + expr1.registradorFinal();
		}
		sb.append("LD R" + regFinal + ", " + s1);
		sb.append(System.lineSeparator());
		
		if (expr2 instanceof Literal) {
			Literal l = (Literal) expr2;
			for (int i = 0; i < l.getValor(); i++) {
				sb.append(acao + " R" + regFinal + ", R" + regFinal + ", #2");
				sb.append(System.lineSeparator());
			}
		} else {
		}
		
		return sb.toString();
	}

	@Override
	public Integer registradorFinal() {
		return regFinal;
	}

	@Override
	public boolean possuiRegistradorFinal() {
		return true;
	}

}
